<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends AdminCommonController {
    public function index() {
        $types = M('ArticleType') -> select();
        $this -> assign('types', $types);
        $this -> display();
    }

    public function publish() {
        $rules = array( 
        	array('title', 'require', '请填写标题'), 
        	array('type', 'require', '请选择文章类型'), 
        	array('content', 'require', '请填写文章内容'), 
		);
		error_reporting(E_ERROR|E_WARNING);
        $Article = M('Article');
        if (!$Article -> validate($rules) -> create()) {
            $this -> error($Article -> getError(), '', 2);
        } else {
            $Article -> create_time = date("Y-m-d H:i:s");
            $Article -> last_time = date("Y-m-d H:i:s");
            $Article -> a_id = session("ADMIN_ID");
            $Article -> a_name = session("nickname");
            $Article -> add();
            $this -> success("发布成功！", "alist");
        }
    }

    public function alist() {
        $Article = M('Article');
        $list = $Article -> select();
        $types = M('ArticleType') -> select();

        if (!$list) {
            $this -> error($Article -> getError());
        } else {
            $this -> assign("list", $list);
            $this -> assign('types', $types);
            $this -> display();
        }
    }

    public function alistOfType() {
        $type = I('get.id');
        $Article = M('Article');
		$types = M('ArticleType') -> select();
        $list = $Article -> where('type=%d', array($type)) -> select();
        if (!$list) {
            $this -> error($Article -> getError());
        } else {
            $this -> assign("list", $list);
			$this -> assign('types', $types);
            $this -> display('alist');
        }
    }

    public function edit() {
        $a_id = I("get.a_id");
        $Article = M('Article');
        $data = $Article -> find($a_id);
        $this -> assign("data", $data);
        $types = M('ArticleType') -> select();
        $this -> assign('types', $types);
        $this -> display();
    }

    public function update() {
        $rules = array( 
        	array('title', 'require', '请填写标题'),
        	array('type', 'require', '请选择文章类型'), 
        	array('content', 'require', '请填写文章内容'), 
		);
		error_reporting(E_ERROR|E_WARNING);
        $Article = M('Article');
        if (!$Article -> validate($rules) -> create()) {
            $this -> error($Article -> getError(), '', 2);
        } else {
            $Article -> last_time = date("Y-m-d H:i:s");
            $Article -> save();
            $this -> success("修改成功！", "alist");
        }
    }

	//通用改变状态方法
	public function updateStatus(){
		$id = I("get.id");
        $Model = M('Article');
        $Model -> find($id);
        if($Model -> status == 0){
            $Model -> status = 1;
        } else {
            $Model -> status = 0;
        }
        $Model -> save();
        $this -> success("状态修改成功");
	}
}
